package com.crm.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.base.PageResult;
import com.crm.base.UserVo;
import com.crm.domain.dto.UserDto;
import com.crm.domain.entity.User;
import com.crm.mapper.UserManagementMapper;
import com.crm.service.UserManagementService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserManagementServiceImp extends ServiceImpl<UserManagementMapper, User> implements UserManagementService {

    private final UserManagementMapper userMapper;

    @Override
    public com.crm.base.PageResult<User> getPagedUsers(Integer pageNum, Integer pageSize) {
        Integer total = getTotalUsers();
        List<User> users = getUserList(pageNum, pageSize);

        return new PageResult<>(200, "查询成功", users, total);
    }

    @Override
    public UserVo createUser(UserDto userDto) {
        User user = BeanUtil.toBean(userDto, User.class);
//        user.setCreateTime(LocalDate.now());
        userMapper.insert(user);
        return BeanUtil.toBean(user, UserVo.class);
    }

    @Override
    public List<User> findUsersByDeptId(Long deptId) {
        return userMapper.findUsersByDeptId(deptId);
    }


    public List<User> getUserList(Integer pageNum, Integer pageSize) {
        Integer offset = (pageNum - 1) * pageSize;
        List<User> users = userMapper.selectUserByPage(offset, pageSize);
        return users;
    }

    public Integer getTotalUsers() {
        return userMapper.countUsers();
    }


}
